//
//  LLGHomeTableViewController.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/8.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit

//cell重用标示
let homeTableViewCell = "homeTableViewCell"

class LLGHomeTableViewController: LLGVisitorTableViewController {

    //数据源
    private lazy var statuseListViewModel: LLGStatuseListViewModel = LLGStatuseListViewModel()
    
    //加载新数据视图
    private lazy var pullUpView: UIActivityIndicatorView = {
    
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = UIColor.redColor()
        return view
    }()
    
    //系统下拉刷新
    private lazy var refreshCtr: UIRefreshControl = {
    
        let ctr = UIRefreshControl()
        ctr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        return ctr
    }()
    
    //自定义下拉刷新
    private lazy var pullDownView: LLGRefreshControl = {
    
        let ctr = LLGRefreshControl()
        ctr.addTarget(self, action: "pullDownRefresh", forControlEvents: .ValueChanged)
        return ctr
    }()
    
    //tip文字
    private lazy var tipLabel: UILabel = {
    
        let label = UILabel(fontSize: 12, color: UIColor.whiteColor())
        
        label.backgroundColor = UIColor.orangeColor()
        label.text = "没有加载到微博数据"
        label.textAlignment = .Center
        //默认隐藏
        label.hidden = true
        
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if isLogin {
            
            setupUI()
            
            //加载数据
            loadData()
        
        } else {
        
            visitorView?.updataVistiorViewInfo(nil, imageName: nil)
        }

    }
    
    // MARK: --添加控件设置约束
    private func setupUI() {
    
        //设置tableView
        setupTableView()
        
        //判断导航控制器是否存在,插入tipLabel
        if let nav = self.navigationController {
        
            tipLabel.frame = CGRectMake(0, CGRectGetMaxY(nav.navigationBar.frame) - 30, nav.navigationBar.width, 30)
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
        }
    }
    
    
    // MARK: --设置tableView
    private func setupTableView() {
    
        //注册cell
        tableView.registerClass(LLGStatuseTableViewCell.self, forCellReuseIdentifier: homeTableViewCell)
        
        //自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //预估行高
        tableView.estimatedRowHeight = 200
        
        //去掉分割线
        tableView.separatorStyle = .None
        
        //添加上拉加载控件
        tableView.tableFooterView = pullUpView
        //自适应
        pullUpView.sizeToFit()
        
//        self.refreshControl = refreshCtr
//        tableView.addSubview(refreshCtr)
        
        tableView.addSubview(pullDownView)
    }
    
    // MARK: --上拉刷新
    @objc private func pullDownRefresh() {
    
        loadData()
    }
    
    // MARK: --结束刷新
    private func endRefreshing() {
    
        pullUpView.stopAnimating()
        
        refreshCtr.endRefreshing()
        
        pullDownView.endRefreshing()
    }
    
    // MARK: --开启tipLabel动画开启
    private func startAnimation(message: String) {
        
        if tipLabel.hidden == false {
            return
        }
    
        tipLabel.text = message
        
        tipLabel.hidden = false
        
        UIView.animateWithDuration(1, animations: { () -> Void in
            
            self.tipLabel.transform = CGAffineTransformMakeTranslation(0, self.tipLabel.height)
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.tipLabel.transform = CGAffineTransformIdentity
                    }, completion: { (_) -> Void in
                        self.tipLabel.hidden = true
                })
        }
    }
    
    // MARK: --加载数据
    private func loadData() {
        
        statuseListViewModel.loadData(pullUpView.isAnimating(), callBack: { (isSuccess, message) -> () in
            
            if !self.pullUpView.isAnimating() {
                self.startAnimation(message)
            }
            
            //结束刷新
            self.endRefreshing()
            if isSuccess {
                self.tableView.reloadData()
            } else {
                print("网络异常")
            }
        })
        
    }
}


// tableView数据源方法
extension LLGHomeTableViewController {

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuseListViewModel.dataList.count

    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(homeTableViewCell, forIndexPath: indexPath) as! LLGStatuseTableViewCell
        
        cell.statuseViewModel = statuseListViewModel.dataList[indexPath.row]
        
        //取消选中样式
        cell.selectionStyle = .None
        
        return cell
    }
    
    //即将显示cell
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        //最后一个cell,且没有加载数据
        if indexPath.row == statuseListViewModel.dataList.count - 1 && !pullUpView.isAnimating() {
        
            //开启动画
            pullUpView.startAnimating()
            //加载数据
            loadData()
            
        }
        
    }
    
    
    
}
